﻿
using Ec.NETCoreFramework.Common.Cache;
using System;
using System.Collections.Generic;
using Ec.NETCoreFramework.Common.Core;

namespace Wx.NETCoreFramework.IService
{
    /// <summary>
    /// 企业用户关系服务
    /// </summary>
    public  class UserNameService
    {
        private readonly RedisSetService _cacheService;     
        private readonly IRedisSingleLock _redisLock;

        private static UserNameService _instance;
        static readonly object obj = new object();
        private UserNameService(RedisSetService cacheService, IRedisSingleLock redisSingleLock)
        {
            this._cacheService = cacheService;
            this._redisLock = redisSingleLock;
            BatchPushUserName();
        }
        public static UserNameService GetInstance(RedisSetService cacheService, IRedisSingleLock redisSingleLock)
        {
            if (_instance == null)
            {
                lock (obj)
                {
                    if (_instance == null)
                    {
                        _instance = new UserNameService(cacheService, redisSingleLock);
                    }
                }
            }
            return _instance;
        }
        public UserNameService()
        {
            PopUserName();
        }
        public string USERNAMEQR = "USERNAMEQR";
        /// <summary>
        /// 返回该列表上的第一个元素
        /// </summary>
        /// <returns></returns>
        public  string PopUserName()
        {
            var o = _cacheService.SetPop<string>(USERNAMEQR);
            if (string.IsNullOrEmpty(o))
            {
                BatchPushUserName();

                if (string.IsNullOrEmpty(o))
                {
                    o = _cacheService.SetPop<string>(USERNAMEQR);
                }
            }

            return o;
        }
        /// <summary>
        /// 批量
        /// </summary>
        private  void BatchPushUserName()
        {
            if (_cacheService.IsExitSet(USERNAMEQR)) return;
            _redisLock.BlockingWork(USERNAMEQR, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), async() =>
            {
                //获取到锁以后，也要判断是否已经初始化
                if (_cacheService.IsExitSet(USERNAMEQR)) return FrameworkResponse.Success(0);
                var i = 0;
                var dic = new List<string>();
                while (i < 20000)
                {
                    dic.Add(CreateUserName());
                    i++;
                }
                 _cacheService.BatchSet<string>(USERNAMEQR, dic);
              
                return FrameworkResponse.Success(0);
            });
       
           
        }
        /// <summary>
        /// 生成用户名
        /// </summary>
        /// <returns></returns>
        private  string CreateUserName()
        {

            byte[] buffer = Guid.NewGuid().ToByteArray();

            string ticke = BitConverter.ToInt64(buffer, 0).ToString();
            if (ticke.Length > 5)
            {
                ticke = ticke.Substring(ticke.Length - 5, 5);
            }
            return string.Format("106910{0}", ticke);
        }     
    }
}
